package eproject.mm.com.projectsupervision.tree;

import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import eproject.mm.com.projectsupervision.R;
import me.texy.treeview.TreeNode;
import me.texy.treeview.base.BaseNodeViewBinder;
import me.texy.treeview.base.CheckableNodeViewBinder;

/**
 * Created by zxy on 17/4/23.
 */

public class SecondLevelNodeViewBinder extends BaseNodeViewBinder {
    private static final String TAG = SecondLevelNodeViewBinder.class.getName();

    TextView textView,tv_Label;
    ImageView imageView;
    View _itemView;
    Handler _handler;

    public SecondLevelNodeViewBinder(View itemView, Handler handler) {
        super(itemView);
        _itemView = itemView;
        _handler = handler;

        textView = (TextView) itemView.findViewById(R.id.node_name_view);
        imageView = (ImageView) itemView.findViewById(R.id.arrow_img);
        tv_Label = (TextView) itemView.findViewById(R.id.node_label);
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_second_level;
    }

    @Override
    public void bindView(final TreeNode treeNode) {
        Map vMap = (Map)treeNode.getValue();
        textView.setText(vMap.get("name").toString());
        imageView.setRotation(treeNode.isExpanded() ? 90 : 0);
        if(vMap.containsKey("percentdone")){
            tv_Label.setText((String)vMap.get("percentdone")+"%");
        }
        if(treeNode.getChildren() == null || treeNode.getChildren().size() == 0){
            imageView.setVisibility(View.INVISIBLE);
        }

        if(treeNode.getChildren() == null || treeNode.getChildren().size()==0) {
            _itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Log.d(TAG,treeNode.getValue().toString());
//                    Map map = new HashMap();
//                    map.put("id", "");
//                    map.put("name", treeNode.getValue().toString());
                    _handler.sendMessage(_handler.obtainMessage(5, treeNode.getValue()));
                }
            });
        }
    }

    @Override
    public void onNodeToggled(TreeNode treeNode, boolean expand) {
        if (expand) {
            imageView.animate().rotation(90).setDuration(200).start();
        } else {
            imageView.animate().rotation(0).setDuration(200).start();
        }
    }
}